﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Utils.MVVM.UI;
using Habi.Solver.ViewModels;
using Habi.Solver.Services;
using Habi.Solver.Models;
using DotNetExtensions.AspNet.DevExpress.Services;

namespace Habi.Solver.Views
{
    [ViewType(MainViewModel.RandomView)]
    public partial class RandomView : XtraUserControl
    {
        private IGridViewService gridViewService;
        public RandomView()
        {
            InitializeComponent();
            gridViewService = new GridViewService();
            mvvmContext.ViewModelType = typeof(RandomViewModel);
            mvvmContext.RegisterService(new ClipboardService());
            if (!mvvmContext.IsDesignMode) InitializeChart();

        }
        void InitializeChart()
        {
            var fluent = mvvmContext.OfType<RandomViewModel>();
            fluent.WithEvent(this, "Load").EventToCommand(x => x.Load());

            fluent.SetBinding(chartControlT1.Series[0], serie => serie.DataSource, x => x.Red_T);
            fluent.SetBinding(chartControlT2.Series[0], serie => serie.DataSource, x => x.Blue_T);

            fluent.SetBinding(chartControlS1.Series[0], serie => serie.DataSource, x => x.Red_S);
            fluent.SetBinding(chartControlS2.Series[0], serie => serie.DataSource, x => x.Blue_S);

            fluent.SetBinding(gridControlT, g => g.DataSource, x => x.ResultT);
            fluent.SetBinding(gridControlS, g => g.DataSource, x => x.ResultS);

            fluent.SetTrigger(x => x.Runing, (run) =>
            {
                timerSync.Enabled = run;
                simpleButtonStop.Enabled = run;
                simpleButtonStart.Enabled = !run;
            });

            fluent.WithEvent(simpleButtonCopy, "Click").EventToCommand(x => x.Copy());
            fluent.WithEvent(timerSync, "Tick").EventToCommand(x => x.Tick());

            fluent.BindCommand(simpleButtonStart, x => x.Start());
            fluent.BindCommand(simpleButtonStop, x => x.Stop());
            fluent.BindCommand(simpleButtonIgnore, x => x.Ignore());

            gridViewService.Indicator<RandomResult>(gridControlS);
            gridViewService.Indicator<RandomResult>(gridControlT);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            timerSync.Enabled = true;
        }
    }
}
